package com.xiaobai.zhuangxiuguanjia.activities;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.xiaobai.zhuangxiuguanjia.R;

public class BaiduMapActivity extends Activity implements BDLocationListener, View.OnClickListener {
    private MapView mapView;
    private BaiduMap baiduMap;
    private LocationClient locationClient;
    private LocationClientOption locationClientOption;
    private Marker marker;
    private ImageView ditufanhui;
    private ImageView jujiaodingwei;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_baidumap);
        mapView = (MapView) findViewById(R.id.bmapView);
        jujiaodingwei= (ImageView) findViewById(R.id.jujiaodingwei);
        ditufanhui= (ImageView) findViewById(R.id.ditufanhui);

        jujiaodingwei.setOnClickListener(this);
        ditufanhui.setOnClickListener(this);

        baiduMap =mapView.getMap();


        locationClient = new LocationClient(getApplicationContext());
        locationClient.registerLocationListener(this);
        locationClientOption = new LocationClientOption();
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        locationClientOption.setOpenGps(true);
        locationClientOption.setScanSpan(5000);
        locationClientOption.setCoorType("bd0911");
        locationClientOption.setIsNeedAddress(true);

        locationClient.setLocOption(locationClientOption);

    }

    @Override
    protected void onResume() {
        super.onResume();

        // 通知地图，当前Activity显示了
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();

        // 定位服务的停止（避免一直进行定位操作）
        locationClient.stop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mapView.onDestroy();

    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        int locType = bdLocation.getLocType();
        switch (locType){
            case 61: // GPS 结果
            case 65: // 缓存的结果
            case 161: // 网络定位的结果
                String province = bdLocation.getProvince();
                String city = bdLocation.getCity();
                Log.d("LocationTest", "当前城市:" + city);
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                String addrStr = bdLocation.getAddrStr();
                Log.d("----->", addrStr);
                if(marker==null){
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(latLng);
                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_location_blue);
                    markerOptions.icon(bitmapDescriptor);
                    marker = (Marker) baiduMap.addOverlay(markerOptions);
                    MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng,16);
                    // 设置当前地图状态，更新为新的位置
                    baiduMap.setMapStatus(mapStatusUpdate);
                }
                marker.setPosition(latLng);
                break;
            default:
                break;

        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.jujiaodingwei:

                locationClient.start();


                break;
            case R.id.ditufanhui:
                finish();
                break;
        }
    }
}
